#!/bin/bash
#
# File: script/update_svn.sh
#	update file to svn sandbox
#	Warning: You must know what are you doing when use the bashscript!
#	Or may be, All you need is this:
# 	find . \( -name \*.c -o -name \*.h -o -name Makefile \) -exec \
# 		cp {} ${svn_dir}/src/{} \;
#
# Author: Spark Zheng
# Version: 0.1.537397253
# 

work_dir=${1:-"/home/zhengyu/Projects/mfs_projects/dserver"}
svn_dir=${2:-"/home/zhengyu/svn/my/test-servers/dserver"}

function build_dir_and_copy()
{
	dir_name=$(dirname $1)
	file_name=$(basename $1)

	cp_dir_name=$2/${dir_name}
	cp_file_name=${cp_dir_name}/${file_name}
	
	if [ ! -d "$cp_dir_name" ]
	then
		mkdir -pv ${cp_dir_name}
		cp -fv ${1} ${cp_file_name}
	else
		if [ ! -e "${cp_file_name}" -o "${1}" -nt "${cp_file_name}" ]
		then
			cp -fv ${1} ${cp_file_name}
#			say_hello $cp_file_name
		fi
	fi

	return 0
}

function say_hello()
{
	echo "hello $1"
	return 0
}

if [ -d "$work_dir" -a -d "$svn_dir" ]
then
	:
else
	echo "dir unexpected."
	exit 1	
fi

pushd ${work_dir}
make -C "${work_dir}/src" clean

file_list=$(find . -type f ! -wholename "*/.*" -a ! -wholename "*.[o|a]" \
			-a ! -wholename "*.so" -a ! -wholename "*.la" \
			-a ! -wholename "*.anjuta" -a ! -wholename "*.log" \
			-a ! -wholename "*~" -a ! -wholename "*.out" \
			-a ! -name "dcache" -a ! -name "dcomm\*" -a ! -name "tags")

for i in $file_list
do
	build_dir_and_copy $i ${svn_dir}
#	say_hello $i
done

popd

exit 0

